<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="所属角色">
          <el-select v-model="form.roleid" placeholder="请选择所属角色">
            <el-option
              v-for="(item, index) in roleList"
              :key="index"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input show-password v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="obj.isAdd"
          >确 定</el-button
        >
        <el-button type="primary" @click="edit" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {mapActions,mapGetters} from "vuex"
import {userAdd,userInfo,userEdit} from "../../../request/api"
import {successAlert,warningAlert} from "../../../utils/alert.js"
export default {
  props: ["obj"],
  data() {
    return {
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
    };
  },
  methods: {
      ...mapActions({
          asyncRoleList:"role/asyncChangeList",
          asyncManageList:"manage/asyncChangeList",
          asyncManageCount:"manage/asyncChangeCount"
      }),
    reset() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    cancel() {
      this.obj.isTrue = false;
      this.reset();
    },
    add(){
        userAdd(this.form).then(res=>{
            if(res.data.code==200){
                successAlert(res.data.msg)
                this.cancel()
                //重新请求user列表数据
                this.asyncManageList(),
                //重新请求user列表总数
                this.asyncManageCount()
            }else{
                warningAlert(res.data.msg)
            }
        })
    },
    getInfo(uid){
        userInfo({uid:uid}).then(res=>{
            this.form={
                ...res.data.list,
                uid:uid,
                password:""
            }
        })
    },
    edit(){
        userEdit(this.form).then(res=>{
            if(res.data.code==200){
                successAlert(res.data.msg)
                this.cancel()
                this.asyncManageList()
            }else{
                warningAlert(res.data.msg)
            }
        })

    }
  },
  computed:{
      ...mapGetters({
          roleList:"role/list",
          manageList:"manage/list"
      })
  },
  mounted(){
      this.asyncRoleList(),
      this.asyncManageList()
  }
};
</script>

<style>
</style>